package com.study.web.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.study.app.domain.bo.AppUserFeedbackBo;
import com.study.app.domain.vo.AppUserFeedbackVo;
import com.study.app.service.IAppUserFeedbackService;
import com.study.common.annotation.Log;
import com.study.common.annotation.RepeatSubmit;
import com.study.common.core.controller.BaseController;
import com.study.common.core.domain.PageQuery;
import com.study.common.core.domain.R;
import com.study.common.core.page.TableDataInfo;
import com.study.common.core.validate.AddGroup;
import com.study.common.core.validate.EditGroup;
import com.study.common.enums.BusinessType;
import com.study.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户问题反馈
 *
 * @author study
 * @date 2023-09-20
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/userFeedback")
public class AppUserFeedbackController extends BaseController {

    private final IAppUserFeedbackService iAppUserFeedbackService;

    /**
     * 查询用户问题反馈列表
     */
    @SaCheckPermission("system:userFeedback:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserFeedbackVo> list(AppUserFeedbackBo bo, PageQuery pageQuery) {
        return iAppUserFeedbackService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户问题反馈列表
     */
    @SaCheckPermission("system:userFeedback:export")
    @Log(title = "用户问题反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserFeedbackBo bo, HttpServletResponse response) {
        List<AppUserFeedbackVo> list = iAppUserFeedbackService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户问题反馈", AppUserFeedbackVo.class, response);
    }

    /**
     * 获取用户问题反馈详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:userFeedback:query")
    @GetMapping("/{id}")
    public R<AppUserFeedbackVo> getInfo(@NotNull(message = "主键不能为空")
                                        @PathVariable Long id) {
        return R.ok(iAppUserFeedbackService.queryById(id));
    }

    /**
     * 新增用户问题反馈
     */
    @SaCheckPermission("system:userFeedback:add")
    @Log(title = "用户问题反馈", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserFeedbackBo bo) {
        return toAjax(iAppUserFeedbackService.insertByBo(bo));
    }

    /**
     * 修改用户问题反馈
     */
    @SaCheckPermission("system:userFeedback:edit")
    @Log(title = "用户问题反馈", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserFeedbackBo bo) {
        return toAjax(iAppUserFeedbackService.updateByBo(bo));
    }

    /**
     * 删除用户问题反馈
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:userFeedback:remove")
    @Log(title = "用户问题反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iAppUserFeedbackService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 完成用户问题反馈信息确认
     *
     * @param id
     * @return
     */
    @SaCheckPermission("system:userFeedback:processed")
    @PostMapping("/processed")
    public R<Void> changeProcessed(@NotNull(message = "主键不能为空")
                                   @RequestParam Long id, String contentReply) {
        return toAjax(iAppUserFeedbackService.changePro(id, contentReply));
    }
}